package com.sc.nft.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dto.CollectionEcologyCreateVO;
import com.sc.nft.dto.TeamHoldCollectionVO;
import com.sc.nft.dto.UserHoldCollectionDTO;
import com.sc.nft.dto.UserHoldVO;
import com.sc.nft.entity.UserCollection;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.BoxGroupListDTO;
import com.sc.nft.entity.dto.CollectionLogDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.entity.vo.metaWalletDTO.SimpleUserCollectionVO;
import com.sc.nft.enums.CollectionOnConsignEnum;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 用户藏片表(sc_user_collection)数据Mapper
 *
 * @author zxy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-06-04 01:42:17
 */
@Mapper
public interface UserCollectionMapper extends BaseMapper<UserCollection> {

    Page<MyCollectionNowVO> myCollectionNow(IPage page, @Param("userId") Long userId, @Param("isPrivilege") String isPrivilege);

    List<MyCollectionNowVO> myCollectionNowList(@Param("userId") Long userId);

    MyCollectionNowVO countMyCollectionNow(@Param("userId") Long userId, @Param("productId") Long productId, @Param("taskPoolId") Long taskPoolId);

    Page<MyCollectionVO> myCollection(IPage page, @Param("userId") Long userId, @Param("isPrivilege") String isPrivilege);

    Page<MyBoxVO> myBox(IPage page, @Param("userId") Long userId, @Param("giveStatus") Integer giveStatus);

    Page<MyBoxVO> myBoxByGiveStatus(IPage page, @Param("userId") Long userId, @Param("giveStatus") Integer giveStatus);

    Page<CollectionLogDTO> giveCollectionLog(IPage page, @Param("userId") Long userId);

    Page<CollectionLogDTO> getCollectionLog(IPage page, @Param("userId") Long userId);

    List<NotOpenBlindBoxVO> notOpenBlindBox(@Param("userId") Long userId, @Param("boxId") Long boxId);

    List<UserCollection> getAllPrivilegeByUserId(@Param("userId") Long userId);

    Page<MyCollectionListByCollectionIdVO> myCollectionListByCollectionId(@Param("collectionId") Long collectionId, @Param("userId") Long userId, Page page);

    /**
     * 后台市值账号批量出售专用
     *
     * @param collectionId
     * @param userId
     * @param page
     * @return
     */
    Page<MyCollectionListByCollectionIdVO> maintainCollectionListByCollectionId(@Param("collectionId") Long collectionId,
                                                                                @Param("userId") Long userId, @Param("lowPrice")
                                                                                BigDecimal lowPrice,
                                                                                @Param("highPrice") BigDecimal highPrice,
                                                                                @Param("onConsign") CollectionOnConsignEnum onConsign, Page page);


    List<UserCollection> getNoNumbering();

    void updateNumbering(@Param("id") Long id, @Param("numbering") int numbering);

    BigDecimal getIssuePrice(@Param("collectionId") Long collectionId, @Param("userId") Long userId);

    List<ConsignCollectionListVO> getConsignCollectionList(@Param("userId") Long userId, @Param("collectionId") Long collectionId);

    List<ConsignCollectionListVO> getConsignCollectionListAdmin(@Param("userId") Long userId, @Param("collectionId") Long collectionId, Integer sum);

    Page<TransferVO> transferLog(Page page, @Param("fromUserAddress") String fromUserAddress, @Param("toUserAddress") String toUserAddress, @Param("transactionHash") String transactionHash, @Param("chainUrl") String chainUrl);

    Page<UserCollectionVO> userCollection(Page page, @Param("firstTitle") String firstTitle, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("userId") Long userId);

    List<UserCollectionExcelVO> userCollectionExcel(@Param("firstTitle") String firstTitle, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("userId") Long userId);

    Page<ConsignCollectionListByNewVO> getPageConsignCollectionListByQuery(Page page, @Param("userId") Long userId, @Param("collectionId") Long collectionId, @Param("antiHoardFlag") Integer antiHoardFlag, @Param("antiHoardPriceSort") Integer antiHoardPriceSort, @Param("charge") BigDecimal charge);


    /**
     * 获取用户城主权益卡
     *
     * @return
     */
    UserCollection getUserApplyCreatedDaoCastellanRights(@Param("userId") Long userId, @Param("collectionId") Long collectionId);


    BigDecimal getUserCountWithPriceAndCollectionIds(@Param("minPrice") BigDecimal minPrice, @Param("maxPrice") BigDecimal maxPrice, @Param("collectionId") List<Long> collectionId);

    BigDecimal getSumPriceWithPriceAndCollectionIds(@Param("minPrice") BigDecimal minPrice, @Param("maxPrice") BigDecimal maxPrice, @Param("collectionId") List<Long> collectionId);

    Integer getNewAddCount(@Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("level") int level);

    /**
     * 分页获取用户持有的藏品
     * 持有  is_hold = 1
     * 开启  is_open = 1
     * 未上架二级 on_consign = 0
     *
     * @param userId
     * @param collectionId
     * @return
     */
    Page<UserHoldCollectionVO> getPageByUserIdAndCollectionId(Page page, @Param("userId") Long userId, @Param("collectionId") Long collectionId);

    /**
     * 获取用户组的持仓成本,比如 城市服务中心下面,他的用户持仓成本是多少
     *
     * @param
     * @return
     */
    BigDecimal getTheTotalPerformanceOfTheUserTeam(@Param("userId") Long userId, @Param("day") Integer day, @Param("urbanServiceCenter") Boolean urbanServiceCenter);

    /**
     * 获取用户的藏品
     *
     * @param page
     * @param ids
     * @return
     */
    Page<UserCollectionBySalvageEventsIdVO> getUserCollectionBySalvageEventsId(Page page, @Param("userId") Long userId, @Param("ids") List<Long> ids);


    List<UserCollection> getNotInUserCollectionChaingList(@Param("collectionId") Long collectionId);

    /**
     * 获取方法userCollection的数据总数，这里因为直接原sql会导致很慢
     *
     * @param firstTitle
     * @param startTime
     * @param endTime
     * @param userId
     * @return
     */
    Long userCollectionTole(@Param("firstTitle") String firstTitle, @Param("startTime") Date startTime, @Param("endTime") Date endTime, @Param("userId") Long userId);

    List<MyCollectionNowListVO> getUserInviteListNow(@Param("userId") Long userId);

    List<MyCollectionNowListVO> getMaintainInviteListNow(@Param("userId") Long userId);

    MyCollectionNowListVO getMaintainCollection(@Param("userId") Long userId, @Param("collectionId") Long collectionId);

    /**
     * 获取传入的用户下级持有某种藏品的用户id
     *
     * @param userId
     * @param collectionIds
     * @return
     */
    List<Long> getJuniorHoldCollectionCountByCollectionIds(@Param("userId") Long userId, @Param("collectionIds") List<Long> collectionIds);


    /**
     * 获取传入的用户持有某种藏品的总和
     *
     * @param userId
     * @param collectionIds
     * @return
     */
    List<UserHoldCollectionDTO> getHoldCountByCollectionIdsAndUserId(@Param("userId") Long userId, @Param("collectionIds") List<Long> collectionIds);

    Integer getFreeUserCount();

    /**
     * 获取用户团对持有某种藏品的总和
     *
     * @param collectionId
     * @param userTel
     * @return
     */
    List<TeamHoldCollectionVO> getTeamHoldCollectioncCountByCollectionId(@Param("collectionId") Long collectionId, @Param("userTel") String userTel);

    /**
     * 网体持仓数量
     *
     * @return
     */
    Integer daoTeamHoldCollectionCount(@Param("daoUserId") Long daoUserId, @Param("collectionId") Long collectionId, @Param("userTel") String userTel);

    /**
     * 网体持仓
     *
     * @return
     */
    List<TeamHoldCollectionVO> daoTeamHoldCollection(@Param("daoUserId") Long daoUserId, @Param("collectionId") Long collectionId, @Param("userTel") String userTel);


    IPage<MyBoxTransferVO> myBoxTransfer(Page objectPage, @Param("userId") Long userId);

    Page<BoxGroupVO> boxGroupUser(Page objectPage, @Param("userId") Long userId);

    BoxGroupVO countBoxGroupUser(@Param("userId") Long userId, @Param("boxId") Long boxId, @Param("taskPoolId") Long taskPoolId);

    /**
     * 获取用户系列盲盒列表
     *
     * @param mp
     * @param boxGroupListDTO
     * @param user
     * @return
     */
    IPage<BoxGroupListVO> boxGroupList(Page mp, @Param("boxGroupListDTO") BoxGroupListDTO boxGroupListDTO, @Param("user") UserInfo user);

    /**
     * 用户持仓（智探）
     *
     * @param page
     * @param userId
     * @return
     */
    Page<ZhiTanUserHoldVO> userHold(Page page, Long userId);

    IPage<MyBoxReceiveVO> myBoxReceive(Page page, @Param("userId") Long userId);

    List<SaasTaskConditionVO> saasTaskCondition(@Param("taskId") Long taskId, @Param("userId") Long userId);

    /**
     * 一键导入用户持仓（仅盯链）
     *
     * @param page
     * @param userId
     * @return
     */
    Page<OneClickImportVO> oneClickImport(Page page, @Param("userId") Long userId);

    @Select(" select * from sc_user_collection  where is_hold=1 and status=0 and is_delete=0 and create_time>'2024-02-02 00:00:00'  and is_open=1  ")
    List<UserCollection> tmp1();

    Integer getPledgeSecretByUserIdAndCollectionId(@Param("userId") Long userId, @Param("collectionId") Long collectionId);


    @Update("update sc_user_collection set task_pool_id=#{takePoolId},is_consume=1 where id=#{id}")
    Integer updateUserCollectionById(@Param("takePoolId") Long takePoolId, @Param("id") Long id);

    /**
     * 获取可以转增到玛特钱包的藏品
     *
     * @param userId
     * @param collectionId
     * @return
     */
    List<SimpleUserCollectionVO> getBatchGiveMetaWalletDetails(@Param("userId") Long userId, @Param("collectionId") Long collectionId);

    List<MyCollectionCountVO> getByUserAndIdList(@Param("userId") Long userId, @Param("collectionIdList") List<Long> collectionIdList);

    /**
     * 获取用户持有的居民数量
     *
     * @return
     */

    int getUpgradeCollectionCountByLevel(@Param("userId") Long userId, @Param("level") int level);

    List<UserCollection> getGoalCollection();

    /**
     * 获取某个用户下面某个时间段内符合条件增长的用户数量
     *
     * @param userId
     * @param startTime
     * @param endTime
     * @return
     */
    Integer getNationalTreasuryIncreaseCount(@Param("userId") Long userId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    /***
     * 可转增商品列表
     * @param page
     * @param userId
     * @param collectionId
     * @param antiHoardPriceSort
     * @return
     */
    Page<GiveCollectionListByNewVO> getPageGiveCollectionListByQuery(Page page, @Param("userId") Long userId, @Param("collectionId") Long collectionId, @Param("antiHoardPriceSort") Integer antiHoardPriceSort);

    @Select("select count(*) from sc_user_collection where is_delete=0 and is_hold=1 and user_id=#{userId} and collection_type=3 ")
    Integer getSumEcologyCollectionCount(@Param("userId") Long userId);

    @Select("select count(*) from sc_user_collection where is_delete=0 and is_hold=1 and is_smelting_only=1 and user_id=#{userId} and collection_type=3")
    Integer getSumEcologyCollectionCountSmelt(@Param("userId") Long userId);

    @Select("select user_id ,count(*) as hold_count from sc_user_collection where is_hold=1 and is_delete=0 and collection_id=#{collectionId} group by user_id")
    List<UserHoldVO> getUserHoldByCollectionId(@Param("collectionId") Long collectionId);

    @Select("select first_title ,count(*) as create_count from sc_user_collection where create_time between #{startTime} and #{endTime} and is_delete=0 and collection_id in (select id from sc_digital_collection where collection_type=3) and type in (7,14,17) group by first_title")
    List<CollectionEcologyCreateVO> getEcologyCollectionCreateCount(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    List<UserCollection> getListByIdListAndDelete(@Param("userCollectionIdList") List<Long> userCollectionIdList);

    Page<MyCopyrightCollectionNowVO> myCopyrightCollectionNow(Page page, @Param("userId") Long userId);


    Page<MallUserCollectionVO> pageMallUserCollection(Page<UserCollection> page, @Param("userId") Long userId, @Param("collectionIds") List<Long> collectionId);


    Page<MallCollectionUserVO> pageUserListByCollectionIds(Page<UserCollection> page, @Param("collectionIds") List<Long> collectionId);

    @Select("select count(*) from sc_user_collection where is_delete=0 and is_hold=1 and is_open=1 and user_id in (select relevance_id from sc_user_relevance where user_id=#{userId}) and collection_id=#{collectionId}")
    Long getUnderHoldCount(@Param("userId") Long userId,@Param("collectionId")Long collectionId);

    List<UserHoldCollectionDTO> getHoldCountByCollectionIdsAndUserIdList(@Param("userIdList") List<Long> userIdList, @Param("collectionIds") List<Long> collectionIds);
}



